<template>
  <div>
    <el-form ref="editPram" :model="editPram" label-width="140px">
      <el-form-item
        :label="$t('fenLeiMingCheng')"
        prop="name"
        :rules="[{ required: true, message: $t('qingShuRuFenLeiMingCheng'), trigger: ['blur', 'change'] }]"
      >
        <el-input v-model="editPram.name" :maxlength="biztype.value === 1 ? 8 : 20" :placeholder="$t('fenLeiMingCheng')" />
      </el-form-item>
      <el-form-item :label="$t('fuJi')">
        <el-cascader
          ref="cascader"
          v-model="editPram.pid"
          :disabled="isCreate === 1 && editPram.pid === 0"
          :options="allTreeList"
          :props="categoryProps"
          style="width: 100%"
          @change="change"
        />
      </el-form-item>
      <!--</el-form-item>-->
      <el-form-item v-if="biztype.value === 5" :label="$t('caiDanTuBiao')">
        <el-input v-model="editPram.icon" :placeholder="$t('qingXuanZeCaiDanTuBiao')">
          <el-button slot="append" icon="el-icon-circle-plus-outline" @click="addIcon" />
        </el-input>
      </el-form-item>
      <el-form-item :label="$t('fenLeiTuBiao_180180')">
        <div class="upLoadPicBox" @click="modalPicTap('1')">
          <div v-if="editPram.icon" class="pictrue">
            <img :src="editPram.icon">
          </div>
          <div v-else class="upLoad">
            <i class="el-icon-camera cameraIconfont" />
          </div>
        </div>
      </el-form-item>
      <el-form-item :label="$t('paiXu')">
        <el-input-number v-model="editPram.sort" :min="1" :max="9999" />
      </el-form-item>
      <!--<el-form-item label="状态">-->
      <!--<el-switch v-model="editPram.status"  active-text="显示"-->
      <!--inactive-text="隐藏" :active-value="true" :inactive-value="false" />-->
      <!--</el-form-item>-->
      <el-form-item v-if="biztype.value !== 1 && biztype.value !== 3 && biztype.value !== 5" :label="$t('kuoZhanZiDuan')">
        <el-input v-model="editPram.extra" type="textarea" :placeholder="$t('kuoZhanZiDuan')" />
      </el-form-item>
      <el-form-item>
        <el-button
          v-hasPermi="['admin:category:update']"
          type="primary"
          :loading="loadingBtn"
          @click="handlerSubmit('editPram')"
        >{{ $t('confirm') }}</el-button>
        <el-button @click="close">{{ $t('cancel') }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<!--创建和编辑公用一个组件-->
<script>
import * as categoryApi from '@/api/categoryApi.js'
import * as selfUtil from '@/utils/ZBKJIutil.js'
import * as storeApi from '@/api/store.js'
export default {
  // name: "edit"
  props: {
    prent: {
      type: Object,
      required: true
    },
    isCreate: {
      type: Number,
      default: 0
    },
    editData: {
      type: Object
    },
    biztype: {
      type: Object,
      required: true
    },
    allTreeList: {
      type: Array
    }
  },
  data() {
    return {
      loadingBtn: false,
      constants: this.$constants,
      editPram: {
        icon: null,
        name: null,
        pid: 0,
        sort: 0,
        id: 0
      },
      categoryProps: {
        value: 'id',
        label: 'name',
        children: 'childList',
        expandTrigger: 'hover',
        checkStrictly: true,
        emitPath: false
      },
      parentOptions: []
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    change() {
      // this.editPram.level = parseInt(this.$refs['cascader'].getCheckedNodes()[0].level) + 1
    },
    // 点击图标
    addIcon() {
      const _this = this
      _this.$modalIcon(function(icon) {
        _this.editPram.extra = icon
      })
    },
    // 点击商品图
    modalPicTap(tit, num, i) {
      const _this = this
      const attr = []
      this.$modalUpload(
        function(img) {
          if (tit === '1' && !num) {
            _this.editPram.icon = img[0].sattDir
          }
          if (tit === '2' && !num) {
            img.map((item) => {
              attr.push(item.attachment_src)
              _this.formValidate.slider_image.push(item)
            })
          }
        },
        tit,
        'store'
      )
    },
    close() {
      this.$emit('hideEditDialog')
    },
    initEditData() {
      // this.parentOptions = this.changeNodes([...this.allTreeList])
      // this.addTreeListLabelForCasCard(this.parentOptions, 'childList')
      const { icon, name, pid, sort, id } = this.editData
      if (this.isCreate === 1) {
        this.editPram.icon = icon
        this.editPram.name = name
        this.editPram.pid = pid
        this.editPram.sort = sort
        this.editPram.id = id
      } else {
        this.editPram.pid = this.prent.id
      }
    },
    handlerSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) return
        this.handlerSaveOrUpdate(this.isCreate === 0)
      })
    },
    handlerSaveOrUpdate(isSave) {
      if (isSave) {
        this.loadingBtn = true
        storeApi
          .productCategoryAddApi(this.editPram)
          .then((data) => {
            this.$emit('hideEditDialog')
            this.$message.success(this.$t('chuangJianMuLuChengGong'))
            this.$store.commit('product/SET_MerProductClassify', [])
            this.loadingBtn = false
          })
          .catch(() => {
            this.loadingBtn = false
          })
      } else {
        this.loadingBtn = true
        storeApi
          .productCategoryUpdateApi(this.editPram)
          .then((data) => {
            this.$emit('hideEditDialog')
            this.$message.success(this.$t('gengXinMuLuChengGong'))
            this.$store.commit('product/SET_MerProductClassify', [])
            this.loadingBtn = false
          })
          .catch(() => {
            this.loadingBtn = false
          })
      }
    }
  }
}
</script>

<style scoped></style>
